home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / pj64.arc / L1.ASM < prev    next >
Assembly Source File  |  1988-12-16  |  6KB  |  239 lines

  1. ; *** Listing 1 ***
  2. ;
  3. ; Program to illustrate the use of the Read Map register in read mode 0.
  4. ; Animates by copying a 16-color image from VGA memory to system memory,
  5. ; one plane at a time, then copying the image back to a new location
  6. ; in VGA memory.
  7. ;
  8. ; By Michael Abrash 4/2/88
  9. ;
  10. stack    segment    word stack 'STACK'
  11.     db    512 dup (?)
  12. stack    ends
  13. ;
  14. data    segment    word 'DATA'
  15. IMAGE_WIDTH    EQU    4    ;in bytes
  16. IMAGE_HEIGHT    EQU    32    ;in pixels
  17. LEFT_BOUND    EQU    10    ;in bytes
  18. RIGHT_BOUND    EQU    66    ;in bytes
  19. VGA_SEGMENT    EQU    0a000h
  20. SCREEN_WIDTH    EQU    80    ;in bytes
  21. SC_INDEX    EQU    3c4h    ;Sequence Controller Index register
  22. GC_INDEX    EQU    3ceh    ;Graphics Controller Index register
  23. MAP_MASK    EQU    2    ;Map Mask register index in SC
  24. READ_MAP    EQU    4    ;Read Map register index in GC
  25. ;
  26. ; Base pattern for 16-color image.
  27. ;
  28. PatternPlane0    label    byte
  29.     db    32 dup (0ffh,0ffh,0,0)
  30. PatternPlane1    label    byte
  31.     db    32 dup (0ffh,0,0ffh,0)
  32. PatternPlane2    label    byte
  33.     db    32 dup (0f0h,0f0h,0f0h,0f0h)
  34. PatternPlane3    label    byte
  35.     db    32 dup (0cch,0cch,0cch,0cch)
  36. ;
  37. ; Temporary storage for 16-color image during animation.
  38. ;
  39. ImagePlane0    db    32*4 dup (?)
  40. ImagePlane1    db    32*4 dup (?)
  41. ImagePlane2    db    32*4 dup (?)
  42. ImagePlane3    db    32*4 dup (?)
  43. ;
  44. ; Current image location & direction.
  45. ;
  46. ImageX        dw    40    ;in bytes
  47. ImageY        dw    100    ;in pixels
  48. ImageXDirection    dw    1    ;in bytes
  49. data    ends
  50. ;
  51. code    segment    word 'CODE'
  52.     assume    cs:code,ds:data
  53. Start    proc    near
  54.     cld
  55.     mov    ax,data
  56.     mov    ds,ax
  57. ;
  58. ; Select graphics mode 10h.
  59. ;
  60.     mov    ax,10h
  61.     int    10h
  62. ;
  63. ; Draw the initial image.
  64. ;
  65.     mov    si,offset PatternPlane0
  66.     call    DrawImage
  67. ;
  68. ; Loop to animate by copying the image from VGA memory to system memory,
  69. ; erasing the image, and copying the image from system memory to a new
  70. ; location in VGA memory. Ends when a key is hit.
  71. ;
  72. AnimateLoop:
  73. ;
  74. ; Copy the image from VGA memory to system memory.
  75. ;
  76.     mov    di,offset ImagePlane0
  77.     call    GetImage
  78. ;
  79. ; Clear the image from VGA memory.
  80. ;
  81.     call    EraseImage
  82. ;
  83. ; Advance the image X coordinate, reversing direction if either edge
  84. ; of the screen has been reached.
  85. ;
  86.     mov    ax,[ImageX]
  87.     cmp    ax,LEFT_BOUND
  88.     jz    ReverseDirection
  89.     cmp    ax,RIGHT_BOUND
  90.     jnz    SetNewX
  91. ReverseDirection:
  92.     neg    [ImageXDirection]
  93. SetNewX:
  94.     add    ax,[ImageXDirection]
  95.     mov    [ImageX],ax
  96. ;
  97. ; Draw the image by copying it from system memory to VGA memory.
  98. ;
  99.     mov    si,offset ImagePlane0
  100.     call    DrawImage
  101. ;
  102. ; Slow things down a bit for visibility.
  103. ;
  104.     mov    cx,1000h
  105. DelayLoop:
  106.     loop    DelayLoop
  107. ;
  108. ; See if a key has been hit, ending the program.
  109. ;
  110.     mov    ah,1
  111.     int    16h
  112.     jz    AnimateLoop
  113. ;
  114. ; Clear the key, return to text mode, and return to DOS.
  115. ;
  116.     sub    ah,ah
  117.     int    16h
  118.     mov    ax,3
  119.     int    10h
  120.     mov    ah,4ch
  121.     int    21h
  122. Start    endp
  123. ;
  124. ; Draws the image at offset DS:SI to the current image location in
  125. ; VGA memory.
  126. ;
  127. DrawImage    proc    near
  128.     mov    ax,VGA_SEGMENT
  129.     mov    es,ax
  130.     call    GetImageOffset    ;ES:DI is the destination address for the
  131.                 ; image in VGA memory
  132.     mov    dx,SC_INDEX
  133.     mov    al,1        ;do plane 0 first
  134. DrawImagePlaneLoop:
  135.     push    di        ;image is drawn at the same offset in
  136.                 ; each plane
  137.     push    ax        ;preserve plane select
  138.     mov    al,MAP_MASK    ;Map Mask index
  139.     out    dx,al        ;point SC Index to the Map Mask register
  140.     pop    ax        ;get back plane select
  141.     inc    dx        ;point to SC index register
  142.     out    dx,al        ;set up the Map Mask to allow writes to
  143.                 ; the plane of interest
  144.     dec    dx        ;point back to SC Data register
  145.     mov    bx,IMAGE_HEIGHT    ;# of scan lines in image
  146. DrawImageLoop:
  147.     mov    cx,IMAGE_WIDTH    ;# of bytes across image
  148.     rep    movsb
  149.     add    di,SCREEN_WIDTH-IMAGE_WIDTH
  150.                 ;point to next scan line of image
  151.     dec    bx        ;any more scan lines?
  152.     jnz    DrawImageLoop
  153.     pop    di        ;get back image start offset in VGA memory
  154.     shl    al,1        ;Map Mask setting for next plane
  155.     cmp    al,10h        ;have we done all four planes?
  156.     jnz    DrawImagePlaneLoop
  157.     ret
  158. DrawImage    endp
  159. ;
  160. ; Copies the image from its current location in VGA memory into the
  161. ; buffer at DS:DI.
  162. ;
  163. GetImage    proc    near
  164.     mov    si,di    ;move destination offset into SI
  165.     call    GetImageOffset    ;DI is offset of image in VGA memory
  166.     xchg    si,di    ;SI is offset of image, DI is destination offset
  167.     push    ds
  168.     pop    es    ;ES:DI is destination
  169.     mov    ax,VGA_SEGMENT
  170.     mov    ds,ax    ;DS:SI is source
  171. ;
  172.     mov    dx,GC_INDEX
  173.     sub    al,al        ;do plane 0 first
  174. GetImagePlaneLoop:
  175.     push    si        ;image comes from same offset in each plane
  176.     push    ax        ;preserve plane select
  177.     mov    al,READ_MAP    ;Read Map index
  178.     out    dx,al        ;point GC Index to Read Map register
  179.     pop    ax        ;get back plane select
  180.     inc    dx        ;point to GC Index register
  181.     out    dx,al        ;set up the Read Map to select reads from
  182.                 ; the plane of interest
  183.     dec    dx        ;point back to GC data register
  184.     mov    bx,IMAGE_HEIGHT    ;# of scan lines in image
  185. GetImageLoop:
  186.     mov    cx,IMAGE_WIDTH    ;# of bytes across image
  187.     rep    movsb
  188.     add    si,SCREEN_WIDTH-IMAGE_WIDTH
  189.                 ;point to next scan line of image
  190.     dec    bx        ;any more scan lines?
  191.     jnz    GetImageLoop
  192.     pop    si        ;get back image start offset
  193.     inc    al        ;Read Map setting for next plane
  194.     cmp    al,4        ;have we done all four planes?
  195.     jnz    GetImagePlaneLoop
  196.     push    es
  197.     pop    ds        ;restore original DS
  198.     ret
  199. GetImage    endp
  200. ;
  201. ; Erases the image at its current location.
  202. ;
  203. EraseImage    proc    near
  204.     mov    dx,SC_INDEX
  205.     mov    al,MAP_MASK
  206.     out    dx,al    ;point SC Index to the Map Mask register
  207.     inc    dx    ;point to SC Data register
  208.     mov    al,0fh
  209.     out    dx,al    ;set up the Map Mask to allow writes to go to
  210.             ; all 4 planes
  211.     mov    ax,VGA_SEGMENT
  212.     mov    es,ax
  213.     call    GetImageOffset    ;ES:DI points to the start address
  214.                 ; of the image
  215.     sub    al,al        ;erase with zeros
  216.     mov    bx,IMAGE_HEIGHT    ;# of scan lines in image
  217. EraseImageLoop:
  218.     mov    cx,IMAGE_WIDTH    ;# of bytes across image
  219.     rep    stosb
  220.     add    di,SCREEN_WIDTH-IMAGE_WIDTH
  221.                 ;point to next scan line of image
  222.     dec    bx        ;any more scan lines?
  223.     jnz    EraseImageLoop
  224.     ret
  225. EraseImage    endp
  226. ;
  227. ; Returns the current offset of the image in the VGA segment in DI.
  228. ;
  229. GetImageOffset    proc    near
  230.     mov    ax,SCREEN_WIDTH
  231.     mul    [ImageY]
  232.     add    ax,[ImageX]
  233.     mov    di,ax
  234.     ret
  235. GetImageOffset    endp
  236. code    ends
  237.     end    Start
  238.  
  239.